package com.suarte.webapp.action;

import com.suarte.core.Currency;
import com.suarte.core.ExchangeRate;
import com.suarte.core.ExchangeRateType;
import com.suarte.core.service.CurrencyManager;
import com.suarte.core.service.ExchangeRateManager;
import java.io.Serializable;
import java.util.List;
import org.appfuse.service.GenericManager;
import static org.apache.commons.lang.StringUtils.isBlank;

/**
 * @date   Mar 1, 2011
 * @author Ggutierrez
 */
public class ExchangeRateForm extends BasePage implements Serializable {

    private ExchangeRateManager exchangeRateManager;
    private CurrencyManager currencyManager;
    private ExchangeRate exchangeRate = new ExchangeRate();
    private Long id;
    private String stType;
    private Currency local;

    public ExchangeRateForm() {
    }

    public ExchangeRateManager getExchangeRateManager() {
        return exchangeRateManager;
    }

    public void setExchangeRateManager(ExchangeRateManager exchangeRateManager) {
        this.exchangeRateManager = exchangeRateManager;
    }

    public void setCurrencyManager(CurrencyManager currencyManager) {
        this.currencyManager = currencyManager;
    }    

    public ExchangeRate getExchangeRate() {
        return exchangeRate;
    }

    public void setExchangeRate(ExchangeRate exchangeRate) {
        this.exchangeRate = exchangeRate;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getStType() {
        return stType;
    }

    public void setStType(String stType) {
        this.stType = stType;
    }

    public Currency getLocal() {        
        local = currencyManager.findLocal();
        return local;
    }

    public void setLocal(Currency local) {
        this.local = local;
    }

    public String delete() {
        exchangeRateManager.remove(exchangeRate.getId());
        addMessage("exchangeRate.deleted");

        return "list";
    }

    public String edit() {
        local = currencyManager.findLocal();
        if (id != null) {
            exchangeRate = exchangeRateManager.get(id);
        } else {
            exchangeRate = new ExchangeRate();
            exchangeRate.setFromCurrency(local);
        }

        return "edit";
    }

    public String add() {
        local = currencyManager.findLocal();
        exchangeRate = new ExchangeRate();
        exchangeRate.setFromCurrency(local);

        return "add";
    }

    public String save() {
        boolean isNew = (exchangeRate.getId() == null);
        local = currencyManager.findLocal();

        ExchangeRateType type = !isBlank(stType) ? ExchangeRateType.valueOf(stType) : null;
        exchangeRate.setType(type);       
        exchangeRate.setFromCurrency(local);
        
        if (exchangeRate.getFromCurrency() == null){
            String key = "exchangeRate.localCurrency.missing";
            addError(key);
            return "edit";
        }

        if (exchangeRate.getFromCurrency().equals(exchangeRate.getToCurrency())) {
            String key = "exchangeRate.equalCurrency";
            addError(key);
            return "edit";
        }        

        exchangeRateManager.save(exchangeRate);

        String key = (isNew) ? "exchangeRate.added" : "exchangeRate.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }

    public List<Currency> getCurrencies() {
        return currencyManager.findAllButLocal();
    }
}
